package com.iheart.common;
import java.util.*;
import java.io.*;
import java.net.*;

public class PropertiesManager {
	private static Properties prop;
	private static Properties sqlprop;
	private static Properties errorcodeprop;
	private static boolean sqlLog;
	private static boolean progressLog;
	
	
	private static ClassLoader cl;
	private static boolean loadFlag = false;
	private static boolean sqlloadFlag = false;
	private static boolean errorcodeLoadFlag = false;
	public static void PropertiesLoad() {
        try {
		cl = Thread.currentThread().getContextClassLoader();
		if( cl == null )
            cl = ClassLoader.getSystemClassLoader();
        	URL url = cl.getResource( "iweb.properties" );
        	String openPath = url.getPath();
        	prop =  new Properties();
        	//openPath = openPath.replaceAll("/","\\\\\\\\");
        	
        	int i;
        	FileInputStream istream = new FileInputStream(openPath);
        	prop.load(istream);
        	
        	System.out.println("prop LOADING END");
        	//System.setProperties(prop);
        	loadFlag=true;        	
        	sqlLog = Boolean.parseBoolean(prop.getProperty("SQL_LOG"));
        	progressLog = Boolean.parseBoolean(prop.getProperty("PROGRESS_LOG"));
	    } catch (Exception e) {
	    	//System.out.print(url.getPath());
	    	System.out.println(e.toString());
	    }
	}
	
	
	public static String getProperties(String key) {
		if (loadFlag==false) {
			PropertiesLoad();
		}
		if(progressLog){
			System.out.println("System.getProperty(key) : " + prop.getProperty(key));
		}
		return prop.getProperty(key);
	}
	public static void setProperties(String key, String value) {
		prop.setProperty(key, value);
	}
	public static void IwebSQLLoad() {
        try {
    	if (loadFlag==false) {
			PropertiesLoad();
		}
		cl = Thread.currentThread().getContextClassLoader();
		if( cl == null )
            cl = ClassLoader.getSystemClassLoader();
			sqlprop =  new Properties();
			String dbms = getProperties("DBMS");
			if (dbms!=null&&dbms.equals("oracle")) {
				loadsqlprop( "Oracle.DBQuery.properties" );
				loadsqlprop( "Oracle.front.DBQuery.properties" );
			} else if (dbms!=null&&dbms.equals("mysql")) {
				loadsqlprop( "Mysql.DBQuery.properties" );
				loadsqlprop( "Mysql.front.DBQuery.properties" );
			} else if (dbms!=null&&dbms.equals("mssql")) {
				loadsqlprop( "Mssql.DBQuery.properties" );
				loadsqlprop( "Mssql.front.DBQuery.properties" );
			} else {
				loadsqlprop( "DBQuery.properties" );
				loadsqlprop( "front.DBQuery.properties" );
				
			}
        	sqlloadFlag=true;
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    }
	   
	}
	public static void loadsqlprop(String fname) {
		try {
			URL url = cl.getResource( fname );
	    	String openPath = url.getPath();
	    	FileInputStream istream = new FileInputStream(openPath);
	    	sqlprop.load(istream);
		 } catch (Exception e) {
		   	System.out.println(e.toString());
		 }
    	
	}
	public static String getSql(String key) {
		if (sqlloadFlag==false) {
			IwebSQLLoad();
		}
		if(sqlLog){
			System.out.println("sqlprop.getProperty(key) : " + key + ":" +  sqlprop.getProperty(key));
		}else{
			System.out.println("sqlprop.getProperty(key) : " + key  );
		}
		return sqlprop.getProperty(key);
	}
	public static void IwebErrorCodeLoad() {
        try {
		cl = Thread.currentThread().getContextClassLoader();
		if( cl == null )
            cl = ClassLoader.getSystemClassLoader();
        	URL url = cl.getResource( "errCode.properties" );
        	String openPath = url.getPath();
        	errorcodeprop =  new Properties();
        	//openPath = openPath.replaceAll("/","\\\\\\\\");
        	
        	int i;
        	FileInputStream istream = new FileInputStream(openPath);
        	errorcodeprop.load(istream);
        	
        	System.out.println("errCode LOADING END");
        	//System.setProperties(prop);
        	errorcodeLoadFlag=true;
	    } catch (Exception e) {
	    	//System.out.print(url.getPath());
	    	System.out.println(e.toString());
	    }
	   
	}
	public static String getErrorCode(String key) {
		if (errorcodeLoadFlag==false) {
			IwebErrorCodeLoad();
		}
		String msg="";
		try {
			if(progressLog){
				System.out.println("errorcodeprop.getProperty(key) : " + errorcodeprop.getProperty(key));
			}
			//System.out.println("prop.getProperty(key) : " + prop.getProperty(key));
		
			msg = new String(errorcodeprop.getProperty(key).getBytes("ISO-8859-1"),"euc-kr");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return msg;
	}

}
